﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
using eProject.ViewModel;

namespace eProject.View.Validation
{
    public class UniqueItemValidationRule<T1, T2> : AbstractValidationRule
    {
        public IEnumerable<T1> Enumerable { get; set; }

        public delegate T2 ConvertDelegate(T1 item);

        public ConvertDelegate Converter;

        public T2 CurrentValue { get; set; }        

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value != null && value is T2 && Converter != null &&
                (value.Equals(CurrentValue) || Enumerable.Where(t => Converter(t).Equals(((T2)value))).Count() == 0))
            {
                return new ValidationResult(true, null);
            }
            return new ValidationResult(false, ErrorMessage);
        }
    }

    public class UniqueTaskValidationRule : UniqueItemValidationRule<TaskViewModel, string>
    {
    }

    public class UniqueEmployeeValidationRule : UniqueItemValidationRule<EmployeeViewModel, string>
    {
    }    

}
